/*
  *@file         sowini.cpp
  *@brief        Simple Read Write Ini File (Use Windows API)
  *@version      1.2.0
*/
#include "stdafx.h"
#include "MtWIni.h"

#pragma warning(push)
#pragma warning(disable: 4786)
#pragma warning(disable: 4996)
#pragma warning(disable: 4800)

#if defined(_MSC_VER) && (_MSC_VER <= 1200)
#define strcpy_s strcpy
#define sprintf_s sprintf
#define wcscpy_s wcscpy
#define swprintf_s swprintf
#endif

MtWIniA::MtWIniA() 
{
    m_nDefDoubleBitNum = 6;
    SetSection("Defalut");
}


MtWIniA::MtWIniA(const char* szFilePath) 
{
    m_nDefDoubleBitNum = 6;
    LoadFile(szFilePath);
    SetSection("Defalut");
}

MtWIniA::MtWIniA(const char* szFilePath,const char* szSection)
{
    m_nDefDoubleBitNum = 6;
    LoadFile(szFilePath);
    SetSection(szSection);
}

/**
 * @brief LoadFile Ini File
 * @param szFilePath - ini File Path Name Load.
 * @return true is sucess , false is fail.
 */
bool MtWIniA::LoadFile(const char* szFilePath)
{
    ::GetFullPathNameA(szFilePath,MAX_PATH,m_szFilePath,NULL);
    SetSection("Defalut");
    return true;
}

bool MtWIniA::SaveFile()
{
    SetSection("XXX{B1CAE400-AF03-40E4-A1CE-30F2134309DE}XXX");
    DelSection("XXX{B1CAE400-AF03-40E4-A1CE-30F2134309DE}XXX");
    SetSection(m_szSection);
    return true;
}

void MtWIniA::SetSection(const char* szSection)
{
    strcpy_s(m_szSection,szSection);
    return ;
}

bool MtWIniA::DelSection(const char* szSection)
{
    return ::WritePrivateProfileStringA(szSection,NULL,NULL,m_szFilePath);
}

bool MtWIniA::DelKeyName(const char* szKeyName)
{
    return ::WritePrivateProfileStringA(m_szSection,szKeyName,NULL,m_szFilePath);
}

bool MtWIniA::SetInt(const char* szKey,int nVal)
{
    sprintf_s(m_szValTemp,"%d",nVal);
    return  ::WritePrivateProfileStringA(m_szSection,szKey,m_szValTemp,m_szFilePath);
}

int MtWIniA::GetInt(const char* szKey,int nDefVal /*= 0*/)
{
	int nVal = ::GetPrivateProfileIntA(m_szSection,szKey, nDefVal,m_szFilePath);
	return nVal;
}

bool MtWIniA::SetString(const char* szKey,const char* szVal)
{
    return ::WritePrivateProfileStringA(m_szSection,szKey,szVal,m_szFilePath);
}

const char* MtWIniA::GetString(const char* szKey,const char* szDefVal /*= NULL*/)
{
	::GetPrivateProfileStringA(m_szSection,szKey,szDefVal,m_szValTemp,sizeof(m_szValTemp),m_szFilePath);
    if((m_szValTemp[0]==0) && (szDefVal != NULL))
	{
        strcpy_s(m_szValTemp,szDefVal);
		SetString(szKey,szDefVal);
	}
	return m_szValTemp;
}

bool MtWIniA::SetDouble(const char* szKey,double dVal)
{
    switch(m_nDefDoubleBitNum)
    {
    case 1:  {sprintf_s(m_szValTemp,("%.1f"),dVal);break;}
    case 2:  {sprintf_s(m_szValTemp,("%.2f"),dVal);break;}
    case 3:  {sprintf_s(m_szValTemp,("%.3f"),dVal);break;}
    case 4:  {sprintf_s(m_szValTemp,("%.4f"),dVal);break;}
    case 5:  {sprintf_s(m_szValTemp,("%.5f"),dVal);break;}
    case 6:  {sprintf_s(m_szValTemp,("%.6f"),dVal);break;}
    case 7:  {sprintf_s(m_szValTemp,("%.7f"),dVal);break;}
    case 8:  {sprintf_s(m_szValTemp,("%.8f"),dVal);break;}
    case 9:  {sprintf_s(m_szValTemp,("%.9f"),dVal);break;}
    default: {sprintf_s(m_szValTemp,("%.6f"),dVal);break;}
    }
    return  SetString(szKey,m_szValTemp);
}

double MtWIniA::GetDouble(const char* szKey,double dDefVal/*=0*/)
{
    const char* m_pVal = GetString(szKey);
    if( m_pVal[0] != 0)
    {
        double dVal = atof(m_pVal);
        return dVal;
    }
	return dDefVal;
}


/////////////////////////////////////////////////////////////////////////////////////

MtWIniW::MtWIniW() 
{
    m_nDefDoubleBitNum = 6;
    SetSection(L"Defalut");
}

MtWIniW::MtWIniW(const wchar_t* szFilePath) 
{
    m_nDefDoubleBitNum = 6;
    LoadFile(szFilePath);
    SetSection(L"Defalut");
}

MtWIniW::MtWIniW(const wchar_t* szFilePath,const wchar_t* szSection)
{
    m_nDefDoubleBitNum = 6;
    LoadFile(szFilePath);
    SetSection(szSection);
}

bool MtWIniW::LoadFile(const wchar_t* szFilePath)
{
    ::GetFullPathNameW(szFilePath,MAX_PATH,m_szFilePath,NULL);
    SetSection(L"Defalut");
    return true;
}

bool MtWIniW::SaveFile()
{
    SetSection(L"XXX{B1CAE400-AF03-40E4-A1CE-30F2134309DE}XXX");
    DelSection(L"XXX{B1CAE400-AF03-40E4-A1CE-30F2134309DE}XXX");
    SetSection(m_szSection);
    return true;
}

void MtWIniW::SetSection(const wchar_t* szSection)
{
    wcscpy_s(m_szSection,szSection);
    return;
}

bool MtWIniW::DelSection(const wchar_t* szSection)
{
    return ::WritePrivateProfileStringW(szSection,NULL,NULL,m_szFilePath);
}

bool MtWIniW::DelKeyName(const wchar_t* szKeyName)
{
    return ::WritePrivateProfileStringW(m_szSection,szKeyName,NULL,m_szFilePath);
}

bool MtWIniW::SetInt(const wchar_t* szKey,int nVal)
{
    swprintf_s(m_szValTemp,L"%d",nVal);
    return  ::WritePrivateProfileStringW(m_szSection,szKey,m_szValTemp,m_szFilePath);
}



int MtWIniW::GetInt(const wchar_t* szKey,int nDefVal /*= 0*/)
{
	int nVal = ::GetPrivateProfileIntW(m_szSection,szKey, nDefVal,m_szFilePath);
	return nVal;
}

bool MtWIniW::SetString(const wchar_t* szKey,const wchar_t* szVal)
{
    return ::WritePrivateProfileStringW(m_szSection,szKey,szVal,m_szFilePath);
}

 const wchar_t* MtWIniW::GetString(const wchar_t* szKey,const wchar_t* szDefVal /*= NULL*/)
{
    ::GetPrivateProfileStringW(m_szSection,szKey, szDefVal,m_szValTemp,sizeof(m_szValTemp),m_szFilePath);
	if((m_szValTemp[0]==0) && (szDefVal != NULL))
	{
        wcscpy_s(m_szValTemp,szDefVal);
	}
    return m_szValTemp;
}

bool MtWIniW::SetDouble(const wchar_t* szKey,double dVal)
{
    switch(m_nDefDoubleBitNum)
    {
    case 1:  {swprintf_s(m_szValTemp,L"%.1f",dVal);break;}
    case 2:  {swprintf_s(m_szValTemp,L"%.2f",dVal);break;}
    case 3:  {swprintf_s(m_szValTemp,L"%.3f",dVal);break;}
    case 4:  {swprintf_s(m_szValTemp,L"%.4f",dVal);break;}
    case 5:  {swprintf_s(m_szValTemp,L"%.5f",dVal);break;}
    case 6:  {swprintf_s(m_szValTemp,L"%.6f",dVal);break;}
    case 7:  {swprintf_s(m_szValTemp,L"%.7f",dVal);break;}
    case 8:  {swprintf_s(m_szValTemp,L"%.8f",dVal);break;}
    case 9:  {swprintf_s(m_szValTemp,L"%.9f",dVal);break;}
    default: {swprintf_s(m_szValTemp,L"%.6f",dVal);break;}
    }
    return  SetString(szKey,m_szValTemp);
}

double MtWIniW::GetDouble(const wchar_t* szKey,double dDefVal/*=0*/, bool bAddIfUnexist /*= false*/)
{
    const wchar_t* m_pVal = GetString(szKey);
    if( m_pVal[0] != 0)
    {
        double dVal = wcstod(m_pVal,NULL);
        return dVal;
    }
    return dDefVal;
}

#pragma warning(pop)
